<?php
declare (strict_types=1);

namespace app\common\model;

class Message extends Base
{
    protected $type = [
        'reply_time' => 'timestamp'
    ];

    protected function user(): object
    {
        return $this->hasOne(User::class, 'id', 'uid')
            ->field('id,code,nickname,mobile,photo');
    }

    protected function getStatusAttr($value): string
    {
        return match ($value) {
            1 => '未回复',
            2 => '已回复',
            default => '未查看'
        };
    }

    public static function _save($param, $options): array
    {
        try {
            $param['status'] = 2;
            $param['admin_id'] = $options['admin']->id;
            $param['admin_name'] = $options['admin']->admin_name;
            $param['reply_time'] = time();
            parent::update($param, ['id' => $param['id']]);

            return ['code' => 1, 'msg' => '回复成功'];
        } catch (\Exception $e) {

            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function add($param, $user): array
    {
        try {
            $param['uid'] = $user->id;
            parent::create($param);

            return ['code' => 1, 'msg' => '提交成功，请等待平台反馈'];
        } catch (\Exception $e) {

            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}